package com.suncreate.zhtz.iot.tool;

import com.suncreate.zhtz.iot.model.ScWarningStrategy;
import com.suncreate.zhtz.iot.service.ScWarningStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class checkDataUnit {
    @Autowired
    private ScWarningStrategyService scWarningStrategyService;

    //数据异常判断
    public static Map<String,Object> checkData(BigDecimal num, List<ScWarningStrategy> list){
        /**
         * 0 正常
         * 1 轻微异常
         * 2 中等异常
         * 3 严重异常
         */
        Map<String,Object> resultMap = new HashMap<>();

        Integer checkDataStart = DataDictionary.CHECK_DATA_STATUS.NORMAL;
        resultMap.put("checkDataStart",checkDataStart);
        resultMap.put("alarmMessage","正常");

        if(!StringUtils.isEmpty(list) && list.size()>0){
            ScWarningStrategy scWarningStrategy = list.get(0);
            //轻微异常判断
            String weakAlarm = scWarningStrategy.getWeakalarm();
            String[] weakArray = weakAlarm.split("-");
            Boolean weakStart = checkInterval(num,weakArray);
            if(weakStart){
                checkDataStart = DataDictionary.CHECK_DATA_STATUS.MINOR;
                resultMap.put("checkDataStart",checkDataStart);
                resultMap.put("alarmMessage","轻微异常");
            }

            //中等异常判断
            String middleAlarm = scWarningStrategy.getMiddlealarm();
            String[] middleArray = middleAlarm.split("-");
            Boolean middleStart = checkInterval(num,middleArray);
            if(middleStart){
                checkDataStart = DataDictionary.CHECK_DATA_STATUS.MODERATE;
                resultMap.put("checkDataStart",checkDataStart);
                resultMap.put("alarmMessage","中等异常");
            }

            //严重异常判断
            String hardAlarm = scWarningStrategy.getHardalarm();
            String[] hardArray = hardAlarm.split("-");
            Boolean hardStart = checkInterval(num,hardArray);
            if(hardStart){
                checkDataStart = DataDictionary.CHECK_DATA_STATUS.CRITICAL;
                resultMap.put("checkDataStart",checkDataStart);
                resultMap.put("alarmMessage","严重异常");
            }

        }else {
            resultMap.put("checkDataStart",checkDataStart);
            resultMap.put("alarmMessage","未配置阈值范围");
        }
        return resultMap;
    }


    //数据是否落在区间
    public static Boolean checkInterval(BigDecimal num,String[] strArray){
        Boolean checkDataStart = false;
        for (int i = 0; i < strArray.length; i++){
            //逐个判断
            String interval = strArray[i];
            interval = interval.replace(" ", "");
            String first = interval.substring(0,1);
            String last = interval.substring(interval.length()-1);
            Integer commaNum = interval.indexOf(',');
            Integer start = interval.indexOf(first);
            Integer end = interval.indexOf(last);

            BigDecimal min = new BigDecimal(interval.substring(start+1,commaNum));
            BigDecimal max = new BigDecimal(interval.substring(commaNum+1,end));

            //判断前
            //大于小于()
            if("(".equals(first) && ")".equals(last)){
                if(num.compareTo(min) == 1 && num.compareTo(max) == -1){
                    checkDataStart = true;
                }
                //大于等于小于等于[]
            }else if(first.equals("[") && "]".equals(last)){
                if(num.compareTo(min) > -1 && num.compareTo(max) < 1){
                    checkDataStart = true;
                }
                //大于等于小于[)
            }else if(first.equals("[") && ")".equals(last)){
                if(num.compareTo(min) > -1 && num.compareTo(max) == -1){
                    checkDataStart = true;
                }
                //大于小于等于(]
            }else if(first.equals("(") && "]".equals(last)){
                if(num.compareTo(min) == 1  && num.compareTo(max) < 1){
                    checkDataStart = true;
                }
            }
        }

        return checkDataStart;
    }

}

